﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace AIC_PN
{
    public partial class FormSearchCanBo : System.Web.UI.Page
    {
        private static int LoaiCB = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            BindDataToRepeater("", -1);
        }

        protected void BindDataToRepeater(string Search, int LoaiCB)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@parentID", SqlDbType.NVarChar, ParameterDirection.Input, Session["userdonvi"]));
            arrParams.Add(SqlHelper.CreateParameterObject("@Search", SqlDbType.NVarChar, ParameterDirection.Input, Search));
            arrParams.Add(SqlHelper.CreateParameterObject("@LaToChuc", SqlDbType.NVarChar, ParameterDirection.Input, LoaiCB));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, "SELECT CB.ID,TenCanBo,TenDV,ChucVu FROM dbo.tblCanBo CB INNER JOIN dbo.tblDonVi DV ON DV.ID = CB.DonViID " +
                                                                        "WHERE (parentID LIKE @parentID+'%'OR ParentID LIKE+'%;'+@parentID+'%') AND (TenCanBo LIKE '%'+@Search+'%' OR @Search='') AND (CB.LaToChuc = @LaToChuc OR @LaToChuc = -1)", arrParams);
            rptDonVi.DataSource = dt;
            rptDonVi.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDataToRepeater(txtSearch.Text, -1);
        }

        protected void btnTT_CheckedChanged(object sender, EventArgs e)
        {
            LoaiCB = 1;
            BindDataToRepeater(txtSearch.Text, LoaiCB);
        }

        protected void btnCN_CheckedChanged(object sender, EventArgs e)
        {
            LoaiCB = 0;
            BindDataToRepeater(txtSearch.Text, LoaiCB);
        }
    }
}
